package com.vonchange.codegen.utils;

import com.vonchange.headb.core.constant.HeaConstant;
import com.vonchange.headb.utils.string.HStringUtils;

public class OrmUtil {
	/**
	 * 数据库字段名转实体类字段名
	 * 
	 * @param colName
	 * @return 实体类字段名
	 */
	
	public static  String toFiled(String colName) {
		StringBuffer sb = new StringBuffer();
		boolean flag = false;
		for (int i = 0; i < colName.length(); i++) {
			char cur = colName.charAt(i);
			if (cur == '_') {
				flag = true;

			} else {
				if (flag) {
					sb.append(Character.toUpperCase(cur));
					flag = false;
				} else {
					sb.append(cur);
				}
			}
		}
		return HStringUtils.replaceEach(sb.toString(), HeaConstant.CharToNum.NUM, HeaConstant.CharToNum.CHAR);
	}
	/**
	 * 数据库表名转实体名
	 * 
	 * @param tableName
	 * @return 实体名
	 */
	public static String toEntity(String tableName) {
		StringBuffer sb = new StringBuffer();
		boolean flag = false;
		for (int i = 0; i < tableName.length(); i++) {
			char cur = tableName.charAt(i);
			if (cur == '_') {
				flag = true;

			} else {
				if (flag) {
					sb.append(Character.toUpperCase(cur));
					flag = false;
				} else {
					sb.append(cur);
				}

			}
		}
		return HStringUtils.capitalize(sb.toString());
	}
}
